package com.kongwg.exer2;

import org.junit.Test;

import java.io.File;
import java.io.IOException;

/**
 * @author kongwg
 * @create 2020-12-17 19:23
 */
public class FileDemo {
    @Test
    public void test() throws IOException {
        File file = new File("f:/io/file");
        boolean mkdir = file.mkdir();
        if (mkdir) {
            System.out.println("创建file文件夹成功");
        }
        File file1 = new File(file, "haha.txt");
        boolean newFile = file1.createNewFile();
        if (newFile) {
            System.out.println("文件创建成功");
        }

        boolean b = FileDemo.deleteFile(file1);
        if (b) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }

    }

    public static boolean deleteFile(File f) {
        boolean delete = f.delete();
        if (delete) {
            return true;
        }
        return false;

    }


    @Test
    public void test2() {
        //判断指定目录下是否有后缀名为.jpg的文件，如果有输出文件的名字
        File file = new File("f:/io");

        String[] list = file.list();
        for (String s : list) {
            boolean b = s.endsWith(".jpg");
            if (b) {
                System.out.println("存在");
                System.out.println(s);

            }

        }
    }


    @Test
    public void test3() {
        File file = new File("f:/Wegame");
        if (file.isDirectory()) {

            String[] list = file.list();
            for (String s : list){
                System.out.println(s);
            }
        }else {
            System.out.println(file + "是文件");
        }
    }
    @Test
    public void test4() {
        File file = new File("f:/Wegame");

        printWj(file);
    }

    public static void printWj(File file){
        File[] files = file.listFiles();

        for (File f : files){
            if (f.isDirectory()){
                printWj(f);
            }else{
                System.out.println(file.getAbsolutePath());
            }
        }

    }
}
